<?php

namespace application\Controller;

use application\Comm\Excel;
use application\Components\BaseController;
use application\Comm\Request;
use application\Module\Article;
use library\Core\Config;
use library\Core\Db\CDbCriteria;
use application\Model\ArticleModel;
use application\Comm\Func;

class ArticleController extends BaseController
{
    /**
     * @throws \application\Components\CException
     * 列表
     */
    public function actionIndex()
    {
        $articleList = new Article();
        $articles = [];
        $status = Config::getParamsVal('STATUS');
        $category = Config::getParamsVal('ARTICLE_CATEGORY');
        $attribute = $articleList->attribute();
        $select = [
            'id',
            'name',
            'description',
            'category_id',
            'url',
            'status',
            'create_time',
            'update_time',
            'orderby',
        ];
        $dataProvider = $articleList->getList($select);
        foreach ($dataProvider['data'] as $key => $article) {
            $articles[$key]['id'] = $article['id'];
            $articles[$key]['name'] = $article['name'];
            $articles[$key]['url'] = $article['url'];
            $articles[$key]['description'] = $article['description'];
            $articles[$key]['orderby'] = $article['orderby'];
            $articles[$key]['status'] = isset($status[$article['status']]) ? $status[$article['status']] : '';
            $articles[$key]['category_id'] = isset($category[$article['category_id']]) ? $category[$article['category_id']] : '';
            $articles[$key]['create_time'] = date('Y-m-d H:i:s', $article['create_time']);
            $articles[$key]['update_time'] = date('Y-m-d H:i:s', $article['update_time']);
        }

        $this->renderPartial('article/index', [
            'articles' => $articles,
            'attribute' => $attribute,
            'category' => $category,
            'page' => $dataProvider['page'],
        ]);
    }

    /**
     * @param $id
     * 详情
     */
    public function actionView($id)
    {
        $status = Config::getParamsVal('STATUS');
        $criteria = new CDbCriteria();
        $select = [
            'id',
            'name',
            'description',
            'url',
            'category_id',
            'content',
            'status',
            'create_time',
            'update_time',
            'orderby',
        ];
        $criteria->select($select);
        $criteria->addEqCondition('id', $id);
        $articleModel = new ArticleModel();
        $data = $articleModel->getRow($criteria);
        $this->renderPartial('article/view', [
            'data' => $data,
            'status' => $status,

        ]);
    }


}
